<?php
require_once("../data/db_connection.php");

$db = new db();
$user_id = $_GET['user_id'];
?>
<html>
<head>模拟-添加创建一个活动，活动发起人为url后面跟着的user_id
<style type="text/css">
#uninvt_events, #rolling_events, #unstart_events
  {
  font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
  width:50%;
  border-collapse:collapse;
  }

#uninvt_events td, #uninvt_events th, #rolling_events td, #rolling_events th, #unstart_events td, #unstart_events th
  {
  font-size:1em;
  border:1px solid #98bf21;
  padding:3px 7px 2px 7px;
  }

#uninvt_events th, #rolling_events th, #unstart_events th
  {
  font-size:1.1em;
  text-align:left;
  padding-top:5px;
  padding-bottom:4px;
  background-color:#A7C942;
  color:#ffffff;
  }

#uninvt_events tr.alt td , #rolling_events tr.alt td, #unstart_events tr.alt td
  {
  color:#000000;
  background-color:#EAF2D3;
  }
</style>
</head>
<body>
	<form id="form1" name="add_event_form" action="" method="post">
		活动地点id:<input type="text" name="location_id" /><br />
		活动地点别名：<input type="text" name="location_byname" /><br />
		选择你要在这个活动中想要邀请的好友：<br />
<?php
	$sql = "SELECT pFriendId FROM pTableRelation WHERE pUserId = " . $user_id . " and pRelationState = 0"; 
	if ($result = $db->query($sql)) {
		while ($row = $result->fetch_assoc()) {
			echo 	"<input type='checkbox' name='invt_friends[]' value='" . 
				$row['pFriendId'] . "' />" . $row['pFriendId'] . "<br />";		
		}
	}
?>
		<input type="submit" name="Submit" value="创建新活动" />
	</form>
<?php
	// 先在pTableEvent插入记录，返回记录的系统id
	$start_date = date("Y-m-d H:i:s");
	$duration_date = date("Y-m-d H:i:s");
	$location_id = $_POST['location_id'];
	$location_byname = $_POST['location_byname'];
	$sql = "INSERT INTO pTableEventInfo VALUES (NULL, 1, '".$start_date."', '".
				$duration_date."', ".$location_id.", '".$location_byname."', ".$user_id.")";
	//echo "sql = " . $sql . "<br />";
	$return_id = $db->insert_get_id($sql);
	if ($return_id) {
		$sql = "INSERT INTO pTableUserEvent VALUES(NULL, ".$user_id.",".$return_id.",0)";
		$return_id1 = $db->insert_get_id($sql);
		echo "新建活动成功,id=".$return_id1."<br />";	
	} else {
		echo "新建活动失败！<br />";
	}
	
	// 得到活动id后，向好友发出活动邀请
	if ($return_id) {
		$invt_friends = $_POST['invt_friends'];
		$invts_count = sizeof($invt_friends);
		foreach($invt_friends as $elem) {
			$sql = "INSERT INTO pTableUserEvent VALUES(NULL, ".$elem.", ".$return_id.", 1);";
			$invt_id = $db->insert_get_id($sql);
			if ($invt_id > 0) {
				$invts_count--;
			}
		}
		if ($invts_count == 0) {
			echo "你创建的活动，已经向你的好友发出邀请了，请等待接受";
		}
	}
	
?>
<h2>未接受邀请的活动</h2>
<?php 
$sql = "SELECT * FROM pTableEventInfo WHERE _id IN ( 
					SELECT pEventId FROM pTableUserEvent
					WHERE pUserId = ".$user_id." and pIfConfirmInvite = 1)";
?>
<table id="uninvt_events">
	<tr><th>活动</th><th>开始时间</th><th>持续时间</th><th>状态</th><th>地点</th><th>别名</th></tr>
<?php
	
	$count = 0;
	if ($result = $db->query($sql)) {
		while($row = $result->fetch_assoc()) {
			if($count % 2 == 0) {
				echo "<tr class='alt'><td>".$row['_id']."</td><td>".
								 $row['pEventStart']."</td><td>".
								 $row['pEventDuration']."</td><td>".
								 $row['pEventState']."</td><td>".
								 $row['pEventLocationId']."</td><td>".
								 $row['pEventLocationByname']."</td></tr>";
			} else {
				echo "<tr><td>".$row['_id']."</td><td>".
								 $row['pEventStart']."</td><td>".
								 $row['pEventDuration']."</td><td>".
								 $row['pEventState']."</td><td>".
								 $row['pEventLocationId']."</td><td>".
								 $row['pEventLocationByname']."</td></tr>";
			}
		}
	}
?>
</table>
<h2>正在进行的活动</h2>
<?php
$sql = "SELECT * FROM pTableEventInfo WHERE pEventState = 2 and _id IN (
				SELECT pEventId FROM pTableUserEvent
				WHERE pUserId = ".$user_id." and pIfConfirmInvite IN (0, 2));";	//2表示正在进行
?>
<table id="rolling_events">
	<tr><th>活动</th><th>开始时间</th><th>持续时间</th><th>状态</th><th>地点</th><th>别名</th></tr>
<?php
	
	$count = 0;
	if ($result = $db->query($sql)) {
		while($row = $result->fetch_assoc()) {
			if ($count % 2 == 0) {
				echo "<tr class='alt'><td>".$row['_id']."</td><td>".
								 $row['pEventStart']."</td><td>".
								 $row['pEventDuration']."</td><td>".
								 $row['pEventState']."</td><td>".
								 $row['pEventLocationId']."</td><td>".
								 $row['pEventLocationByname']."</td></tr>";
			} else {
				echo "<tr><td>".$row['_id']."</td><td>".
								 $row['pEventStart']."</td><td>".
								 $row['pEventDuration']."</td><td>".
								 $row['pEventState']."</td><td>".
								 $row['pEventLocationId']."</td><td>".
								 $row['pEventLocationByname']."</td></tr>";	
			}	
		}
	}
?>
</table>
<h2>未开始</h2>
<?php
$sql = "SELECT * FROM pTableEventInfo WHERE pEventState = 1 and _id IN (
				SELECT pEventId FROM pTableUserEvent
				WHERE pUserId = ".$user_id." and pIfConfirmInvite IN (0, 2));";	//2表示正在进行
?>
<table id="unstart_events">
	<tr><th>活动</th><th>开始时间</th><th>持续时间</th><th>状态</th><th>地点</th><th>别名</th></tr>
<?php
	
	$count = 0;
	if ($result = $db->query($sql)) {
		while($row = $result->fetch_assoc()) {
			if ($count % 2 == 0) {
				echo "<tr class='alt'><td>".$row['_id']."</td><td>".
								 $row['pEventStart']."</td><td>".
								 $row['pEventDuration']."</td><td>".
								 $row['pEventState']."</td><td>".
								 $row['pEventLocationId']."</td><td>".
								 $row['pEventLocationByname']."</td></tr>";
			} else {
				echo "<tr><td>".$row['_id']."</td><td>".
								 $row['pEventStart']."</td><td>".
								 $row['pEventDuration']."</td><td>".
								 $row['pEventState']."</td><td>".
								 $row['pEventLocationId']."</td><td>".
								 $row['pEventLocationByname']."</td></tr>";	
			}	
		}
	}
?>
</table>
</body>
</html>
